<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/mb/jsp/common.jsp" %>

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>豆豆商城</title>
    <!--pc版更改样式  -->
    <style>
    	.kmImg {
		    width: 195px;
		    height:132px;
		}
		
		.addMin input[type="button"] {
		    width: 42px;
		}
		.addMin input[type="text"] {
	    	width: 111px;
		}
    </style>
</head>
<body ontouchstart="">
<div class="w100 db jz" style="padding-bottom: 0;padding-top: 46px;">

    <div id="inner-container" class="w100">

        <div id="resText"></div><!--resText-->
        <div id="loading" class="w100">
            <img src="mb/images/loading.gif" alt="正在加载" class="db jz hauto" style="width: 15px;">
            <div class="w100 tc f10 c9">正在加载</div>
        </div>
        
        <div id="contentId"></div>
        

</div>

 <script>


    /**
     * 加载新数据
     */
    function getData() {
        $.ajax({
            url: "PlugServlet",
            data: JSON.stringify({m: 1670}),
            type: 'post',
            dataType: 'json',
            contentType: "application/json;charset=utf-8",
            beforeSend: function () {
                showProgressDg();
            },
            success: function (data) {
            	
               if (data.state == 1) {
                  showData(data.list);
                } else {
                    // 其他错误弹出提示
                    layer.alert(data.info);
                }
            },
            error: function (data) {
                layer.alert(data.info);
            },
            complete: function () {
                closeProgressDg();
            }
        });
    }
 

    function showData(list)
    {
    	console.log(list);
    	var htmlStr="";
    	$.each(list, function(index, obj) 
    	{
    		var tmpimg=obj.prestigeimage.split(";")[0];
    		var projectid = obj.prestigeid;

    		if(obj.prestigeintro == ""||obj.prestigeintro == undefined||obj.prestigeintro == null){
    		   obj.prestigeintro = "暂无";
    		};
    		var num = obj.limitenum - obj.buynum;
    		var btnBlock = '<button class="w100 tc cf f15 modifyBtn"  onclick="buySth('+ projectid +')">购买</button>';
    		if(num == 0){
    		   num = "0(无库存)";
    		   btnBlock = '<button class="w100 tc cf f15 modifyBtn" style="background:#ddd" onclick="javascript:void(0);">不可购买</button>';
    		}
    		else if(num < 0){
    		  num = "error";
    		  btnBlock = '<button class="w100 tc cf f15 modifyBtn" style="background:#ddd" onclick="javascript:void(0);">不可购买</button>';
    		}

			var FloatAtrr ="";
			var outerDiv="";
			var outerDivEnd = "";
			if(index%2 == 0){
				FloatAtrr="fl";
				outerDiv='<div class="w100 of pdt10 clearfix">';
			}else{
				FloatAtrr="fr";
				outerDivEnd = '</div>';
			}	
    		htmlStr=htmlStr+
				outerDiv+
	    		   	'<section class="proSection pr bcf '+FloatAtrr+'" style="width:49%;">'+
					   '<div class="w100">'+
				            '<article class="of clearfix jz">'+
				                '<div class="fl" style="margin-left:2%;">'+
				                  '<div class="kmImg mgt15 of">'+
				                  	'<img src="'+getBaseImgUrl(tmpimg)+'" class="w100 hauto" onclick=window.location.href="pc/huiGouShangChengDetail.jsp?prestigeid=' + projectid + '">'+
				                  '</div>'+
				                  '<div class="addMin clearfix of">'+
				                  	'<input class="min" id="min' + projectid + '" name="" type="button" value="-" />'+  
									'<input class="num_box" id="num_box' + projectid + '" name="" type="text" value="1" onkeyup="NumCheck(this)"/>'+ 
									'<input class="add" id="add' + projectid + '" name="" type="button" value="+" />  '+
							      '</div>'+
				                '</div>'+
			 
								'<a href="pc/huiGouShangChengDetail.jsp?prestigeid=' + projectid + '" class="fr dib" style="margin:0 2%;width:49%;">'+
				                    '<h5 class="c3 f13 mgt10 lh2">'+obj.prestigename +' </h5>'+
				                    '<p class="c9 f12 lh2">狸猫豆：'+obj.requirevalue+'&nbsp;&nbsp;&nbsp;&nbsp;市场价:<span class="lineThroughSpan">'+obj.oldprice+'</span></p>'+
				                    '<p class="c9 f12 lh18">回购价：'+obj.callbackprice+'</p>'+
				                    '<p class="c9 f12 lh18">剩余数量：'+num+'</p>'+
				                    '<p class="c9 f12 lh18" style="margin-top: 3px;">已售：'+obj.buynum+'</p>'+
				                    '<p class="c9 f12 lh18" style="margin-top: 3px;">简介：'+obj.prestigeintro+'</p>'+
				                '</a>'+
			 
			
				            '</article>'+
				        '</div>'+
				        '<div style="width:96%;border-top:1px solid #ececec;padding:7px 0;" class="jz mgt10">'+
				           btnBlock+
				        '</div>'+
					'</section>'+
					outerDivEnd;
    		
    	});
    	
    	 $("#contentId").html(htmlStr);
    	 countNum();
    }
   
   
     
       // 购买
	function buySth(prestigeid) {
	var nid =  "#num_box"+prestigeid;
	var num = $(nid).val();
        $.ajax({
            url: "PlugServlet",
            data: JSON.stringify({m: 1672,prestigeid:prestigeid,num:num}),
            type: 'post',
            dataType: 'json',
            contentType: "application/json;charset=utf-8",
            beforeSend: function () {

            },
            success: function (data) {
 				console.log(data);
               if (data.state == 1) {
                  // 成功弹出提示
                  layer.alert(data.info);
                } else {
                    // 其他错误弹出提示
                   layer.alert(data.info);
                }
            },
            error: function (data) {
                layer.alert(data.info);
            },
            complete: function () {
                closeProgressDg();
            }
        });
    } 
     
     
     function NumCheck(t){         
          var num = t.value;
          var re = /^[1-9]\d*$/;   //^匹配字符串开始位置   \d 匹配任意一个十进制数字，等价于[0-9]  * 匹配0次 1次或者多次前面的字符  $匹配字符串的结束位置  
          if(!re.test(num)||num == NaN||num == undefined){  
           console.log(num); 
               t.value = "1";
               
     		}  
     }
     function countNum(){
     
      $(".min").attr('disabled',true);
	    //数量增加操作
	    $(".add").click(function(){
	     var t = $(this).siblings(".num_box"); 
	       t.val(parseInt(t.val())+1)
	        if (parseInt(t.val())!=1){
	           $(this).siblings(".min").attr('disabled',false);
	        }
	      
	    })
	    
	     //数量减少操作
	   $(".min").click(function(){
	     var t = $(this).siblings(".num_box"); 
	        t.val(parseInt(t.val())-1);
	        if (parseInt(t.val())==1){
	           $(this).attr('disabled',true);
	        }
	      
	    }) 

     }
    
	$(document).ready(function(){
	
	getData();
	  
	});
</script>


</body>
</html>
